package org.jeecg.modules.app.v1.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.v1.device.entity.*;
import org.jeecg.modules.aspect.ApiLoginAspct;
import org.jeecg.modules.power.device.entity.YlDevice;
import org.jeecg.modules.power.device.entity.YlDeviceRecords;
import org.jeecg.modules.power.device.service.IYlDeviceRecordsService;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.shop.entity.YlShop;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* @Description: 设备变动记录表
* @Author: jeecg-boot
* @Date:   2024-07-17
* @Version: V1.0
*/
@Api(tags="APP管理端_设备变动记录")
@RestController
@RequestMapping("/api/v1/records")
@Slf4j
@RequiredArgsConstructor
public class ApiDeviceRecordsController {

   private final IYlDeviceRecordsService ylDeviceRecordsService;
   private final IYlDeviceService ylDeviceService;
    private final IYlShopService shopService;


    @ApiLogin
    @ApiOperation(value="1_扫码校验设备是否可退库、是否可报修、是否可转移", notes="扫码校验设备是否可退库、是否可转移")
    @GetMapping(value = "/check")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
        @ApiImplicitParam(value = "设备编号",name = "deviceNo"),
    })
    public Result<?> check(String deviceNo,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        JSONObject result = ylDeviceRecordsService.getDevice(deviceNo,bd.getId());
        if(oConvertUtils.isEmpty(result))return Result.error_599("未查询到该设备信息");
        return Result.OK(result);
    }

    /**
     *  提交入库申请
     * @param deviceMac
     * @param storeId
     * @return
     */
    @ApiLogin
    @ApiOperation(value="2_设备正常退库申请", notes="设备正常退库申请")
    @PostMapping(value = "/storage")
    @ApiOperationSupport(order = 2)
    public Result<String> storage(String deviceMac,String storeId,String type,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        ylDeviceRecordsService.storage(deviceMac,storeId,bd,type);
        return Result.OK("提交成功！");
    }

    /**
     *  设备报修入库
     * @param deviceMac
     * @param storeId
     * @return
     */
    @ApiLogin
    @ApiOperation(value="3_设备报修入库申请", notes="设备报修入库申请")
    @PostMapping(value = "/stock")
    @ApiOperationSupport(order =3)
    public Result<String> stock(String deviceMac,String storeId,String type,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        ylDeviceRecordsService.stock(deviceMac,storeId,bd,type);
        return Result.OK("提交成功！");
    }

    /**
     *  提交入库申请
     * @param deviceMac
     * @param bdId
     * @return
     */
    @ApiLogin
    @ApiOperation(value="4_BD间资产转移", notes="4_BD间资产转移")
    @PostMapping(value = "/transfer")
    @ApiOperationSupport(order = 4)
    public Result<String> transfer(String deviceMac,String bdId,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        GroupUser user = ylDeviceService.getGroupUser(bd.getId());
        GroupUser userBd = ylDeviceService.getGroupUser(bdId);
        ylDeviceRecordsService.transfer(deviceMac,userBd,user);
        return Result.OK("提交成功！");
    }



    @ApiLogin
    @ApiOperation(value="6_BD间资产转移列表", notes="BD间资产转移列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索单号",name = "transferNumber"),
            @ApiImplicitParam(value = "1我为接收方2我为借出方",name = "type")
    })
    @GetMapping(value = "/transferList")
    @ApiOperationSupport(order = 4)
    public Result<List<ApiBindDeviceEntity>> transferList(String transferNumber,
                                  @RequestParam(name="type", defaultValue="1")  String type,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiBindDeviceEntity> page = new Page<>(pageNo,pageSize);

        String transferId = Objects.equals(type,"1")?"":bd.getId();
        String receiveId = Objects.equals(type,"2")?"":bd.getId();

        IPage<ApiBindDeviceEntity> pageList = ylDeviceRecordsService.bdDeviceList(page,transferId,receiveId,transferNumber);
        return Result.OK(pageList.getRecords());
    }

    /**
     *  提交入库申请
     * @param deviceMac
     * @param request
     * @return
     */
    @ApiLogin
    @ApiOperation(value="7_BD申请解绑设备", notes="BD申请解绑设备")
    @PostMapping(value = "/unbind")
    @ApiOperationSupport(order = 5)
    public Result<?> unbind(String deviceMac,String shopId,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        YlShop shop = shopService.getById(shopId);
        ylDeviceRecordsService.unbind(deviceMac,bd,shop);
        return Result.OK("解绑成功");
    }

    @ApiLogin
    @ApiOperation(value="8_设备解绑申请单列表", notes="设备解绑申请单")
    @GetMapping(value = "/unbindList")
    @ApiOperationSupport(order = 6)
    public Result<List<ApiUnbindDeviceEntity>> unbindList(String status,String transferNumber,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiUnbindDeviceEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiUnbindDeviceEntity> result = ylDeviceRecordsService.unbindDeviceList(page,status,transferNumber,bd.getId());
        return Result.OK(result.getRecords());
    }

    @ApiLogin
    @ApiOperation(value="11_设备解绑绑定记录", notes="设备解绑绑定记录")
    @GetMapping(value = "/bindList")
    @ApiOperationSupport(order = 7)
    public Result<List<ApiBindDeviceEntity>> bindList(String deviceId,
                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                      HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiBindDeviceEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiBindDeviceEntity> result = ylDeviceRecordsService.bindDeviceList(page,deviceId);
        return Result.OK(result.getRecords());
    }

    @ApiLogin
    @ApiOperation(value="12_设备退库列表", notes="设备解绑绑定记录")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索单号",name = "transferNumber"),
            @ApiImplicitParam(value = "开始时时间",name = "beginTime"),
            @ApiImplicitParam(value = "结束时间",name = "endTime"),
            @ApiImplicitParam(value = "设备类型",name = "deviceCategory"),
            @ApiImplicitParam(value = "退库类型",name = "transferType（2正常退库8故障退库）"),
    })
    @GetMapping(value = "/stockDevice")
    @ApiOperationSupport(order = 8)
    public Result<List<ApiStockDeviceRecordEntity>> stockDeviceList( String transferNumber, String beginTime, String endTime, String deviceCategory, String transferType,
                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                      HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiStockDeviceRecordEntity> page = new Page<>(pageNo,pageSize);
        List<ApiStockDeviceRecordEntity> result = ylDeviceRecordsService.getDeviceRecords(page, bd.getId(), transferNumber, beginTime, endTime, deviceCategory, transferType);
        return Result.OK(result);
    }


    @ApiLogin
    @ApiOperation(value="11_门店下设备解绑绑定记录", notes="门店下设备解绑绑定记录")
    @GetMapping(value = "/shopBindList")
    @ApiOperationSupport(order = 7)
    public Result<List<ApiBindDeviceEntity>> shopBindList(String shopId,String beginTime,String endTime,
                                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                      HttpServletRequest request) {
        Page<ApiBindDeviceEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiBindDeviceEntity> result = ylDeviceRecordsService.shopBindDevice(page,shopId,beginTime,endTime);
        return Result.OK(result.getRecords());
    }

    /**
     *  BD给门店绑定设备
     * @param apiBindDevice
     * @param request
     * @return
     */
    @ApiLogin
    @ApiOperation(value="12_BD给门店绑定设备", notes="BD给门店绑定设备")
    @PostMapping(value = "/bind")
    @ApiOperationSupport(order = 12)
    public Result<?> bind(@RequestBody ApiBindDevice apiBindDevice, HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        YlShop shop = shopService.getById(apiBindDevice.getShopId());
        ylDeviceRecordsService.bindDevice(apiBindDevice,shop,bd);
        return Result.OK("绑定成功");
    }

}
